<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
			<div class="app-space-between">
				<div class="app-space-between-left">
					<el-form-item label="优惠券名称:" prop="couponName">
						<el-input v-model="queryParams.couponName" placeholder="请输入商品类目名称" clearable
							@clear="handleQuery" />
					</el-form-item>

					<el-form-item label="优惠券类型:" prop="couponType">
						<el-select v-model="queryParams.couponType" placeholder="请选择" @change="handleQuery">
							<el-option v-for="item in couponTypeEnum" :key="item.value" :label="item.label"
								:value="item.value" />
						</el-select>
					</el-form-item>
					<!-- 	<el-form-item label="优惠券状态:" prop="title">
						<el-select v-model="queryParams.title" placeholder="请选择">
							<el-option label="item.label" value="item.value" />
						</el-select>
					</el-form-item> -->
					<el-form-item label="优惠券领取时间:" prop="getCouponTimeList">
						<el-date-picker v-model="getCouponTimeList" type="daterange" @change="handleQuery"
							start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd">
						</el-date-picker>
					</el-form-item>

					<el-form-item label="优惠券使用时间:" prop="validiteTimeList">
						<el-date-picker v-model="validiteTimeList" type="daterange" @change="handleQuery"
							start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd">
						</el-date-picker>
					</el-form-item>

				</div>
				<div class="app-space-between-right">
					<el-form-item>
						<el-button type="primary" size="mini" @click="handleQuery">搜索</el-button>
						<el-button size="mini" @click="resetQuery">重置</el-button>
					</el-form-item>
				</div>
			</div>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain size="mini" @click="handleAdd">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="primary" plain size="mini" :disabled="multiple" @click="handleDelete">删除</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
			<el-table-column show-overflow-tooltip type="selection" width="55" align="center" />
			<el-table-column show-overflow-tooltip label="优惠券名称" width="300" align="center" prop="couponName" />
			<el-table-column show-overflow-tooltip label="优惠券类型" align="center" prop="content">
				<template slot-scope="scope">
					{{selectDictLabel(couponTypeEnum, scope.row.couponType)}}
				</template>
			</el-table-column>
			<el-table-column show-overflow-tooltip label="优惠券领取时间" align="center" prop="getCouponStartTime">
				<template slot-scope="scope">
					{{scope.row.getCouponStartTime}}/{{scope.row.getCouponEndTime}}
				</template>
			</el-table-column>
			<el-table-column show-overflow-tooltip label="优惠券使用时间" align="center" prop="validiteStart">
				<template slot-scope="scope">
					{{scope.row.validiteStart}}/{{scope.row.validiteEnd}}
				</template>
			</el-table-column>
			<el-table-column show-overflow-tooltip label="优惠券状态" align="center">
				<template slot-scope="scope">
					{{scope.row | stateNameFilter}}
				</template>
			</el-table-column>
			<el-table-column show-overflow-tooltip label="操作" align="center" width="140" fixed="right">
				<template slot-scope="scope">
					<el-link type="primary" :underline="false" @click="handleUpdate(scope.row)">编辑</el-link>
					<el-link type="primary" :underline="false" style="margin:0 6px;" @click="handleDelete(scope.row)">删除
					</el-link>
				</template>
			</el-table-column>
		</el-table>
		<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
			@pagination="getList" />

		<!-- 增 改 -->
		<couponAdd ref="coupon-add" @success="handleQuery"></couponAdd>
	</div>
</template>

<script>
	import {
		listWcCoupon,
		deleteWcCoupon
	} from "@/api/culturalCreativity/coupon.js";
	import couponAdd from "./components/coupon-add.vue";
	export default {
		components: {
			couponAdd,
		},
		data() {
			return {
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// 岗位表格数据
				postList: [],
				// 弹出层标题
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					couponName: '', // 优惠券名称
					couponType: '', // 优惠券类型
					getCouponStartTime: '', // 优惠券领取开始时间
					getCouponEndTime: '', // 优惠券领取结束时间
					validiteStart: '', // 优惠券使用开始时间
					validiteEnd: '', // 优惠券使用结束时间
				},
				getCouponTimeList: [], // 优惠券领取时间
				validiteTimeList: [], // 优惠券使用时间
				couponTypeEnum: [{
					label: '满减',
					value: '1'
				}, {
					label: '直减',
					value: '2'
				}]
			};
		},
		created() {
			this.getList();
		},
		methods: {
			/** 查询岗位列表 */
			getList() {
				this.queryParams.getCouponStartTime = this.getCouponTimeList[0];
					this.queryParams.getCouponEndTime = this.getCouponTimeList[1];
					this.queryParams.validiteStart = this.validiteTimeList[0];
					this.queryParams.validiteEnd = this.validiteTimeList[1];
					this.loading = true;
				listWcCoupon({},this.queryParams).then(response => {
					this.postList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.validiteTimeList = [];
				this.getCouponTimeList = [];
				this.resetForm("queryForm");
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.couponId)
				this.single = selection.length != 1
				this.multiple = !selection.length
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.$refs['coupon-add'].init();
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				let postId = row.couponId || this.ids
				if (Array.isArray(postId)) postId = postId.toString();
				this.$refs['coupon-add'].init(postId);
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				let postIds = row.couponId || this.ids;
				if (Array.isArray(postIds)) postIds = postIds.toString()
				this.$modal.confirm('此操作将要删除所选的数据，是否继续？').then(function() {
					return deleteWcCoupon(postIds);
				}).then(() => {
					this.getList();
					this.$modal.msgSuccess("删除成功");
				}).catch(() => {});
			}
		},
		filters: {
			stateNameFilter({
				getCouponStartTime,
				getCouponEndTime
			}) {
				if (!(getCouponStartTime && getCouponEndTime)) return 'error'
				let startTime = new Date(getCouponStartTime).getTime();
				let endTime = new Date(getCouponEndTime).getTime();
				let nowTime = Date.now();
				if (nowTime < endTime && nowTime > startTime) return '领取中'
				return '已过期'

			}
		}
	};
</script>
